package com.spic.system.activiti.controller;

import com.spic.common.core.utils.SecurityUtils;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.system.activiti.domain.dto.TaskFinshDTO;
import com.spic.system.activiti.service.IBusinessStatusService;
import com.spic.system.activiti.service.IProcessInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author 290512
 * @create 2021-11-09 16:48
 */
@Api
@RestController
@RequestMapping("/instance")
public class ProcessInstanceController
{
    @Autowired
    private IProcessInstanceService processInstanceService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private IBusinessStatusService businessStatusService;
    /**
     * {
     *     "msg": "操作成功",
     *     "code": 200,
     *     "data": [
     *         {
     *             "processInstanceId": "80684e7e-407a-11ec-9ee6-ba8a201332de",
     *             "taskName": "签发人审批",
     *             "startTime": "2021-11-08 17:59:02",
     *             "endTime": "2021-11-08 18:02:09",
     *             "assignee": "3",
     *             "message": "",
     *             "taskId": "806e1ae4-407a-11ec-9ee6-ba8a201332de",
     *             "status": "已处理"
     *         },
     *         {
     *             "processInstanceId": "80684e7e-407a-11ec-9ee6-ba8a201332de",
     *             "taskName": "许可人审批",
     *             "startTime": "2021-11-08 18:02:09",
     *             "endTime": "2021-11-08 18:47:47",
     *             "assignee": "3",
     *             "message": "",
     *             "taskId": "f0141bef-407a-11ec-b1b3-00ff310f11b1",
     *             "status": "已处理"
     *         },
     *         {
     *             "processInstanceId": "80684e7e-407a-11ec-9ee6-ba8a201332de",
     *             "taskName": "签发人审批",
     *             "startTime": "2021-11-08 18:47:47",
     *             "assignee": "3",
     *             "message": "",
     *             "taskId": "4ff074f9-4081-11ec-b1b3-00ff310f11b1",
     *             "status": "待处理"
     *         },
     *         {
     *             "processInstanceId": "80684e7e-407a-11ec-9ee6-ba8a201332de",
     *             "taskName": "调度员审批",
     *             "startTime": "2021-11-08 18:47:47",
     *             "assignee": "3",
     *             "message": "",
     *             "taskId": "4ff074fb-4081-11ec-b1b3-00ff310f11b1",
     *             "status": "待处理"
     *         }
     *     ]
     * }
     * @param procInstId
     * @return
     */
    @ApiOperation("通过流程实例id获取任务办理历史记录")
    @GetMapping("/history/list") // ?procInstId=xxx
    public AjaxResult getHistoryInfoList(@RequestParam String procInstId) throws Exception
    {
        return AjaxResult.success(processInstanceService.getHistoryInfoList(procInstId));
    }

    @ApiOperation("通过流程实例id获取历史流程图")
    @GetMapping("/history/image") // ?procInstId=xxx
    public void getHistoryProcessImage(@RequestParam String procInstId,
                                       HttpServletResponse response) {
        processInstanceService.getHistoryProcessImage(procInstId, response);
    }

    @ApiOperation("查询正在运行中的流程实例")
    @PostMapping("/list/running")
    public AjaxResult getProcInstRunning() {
        Map<String, Object> result = processInstanceService.getProcInstRunning();
        return AjaxResult.success(result);
    }

    @ApiOperation("查询已结束的流程实例")
    @PostMapping("/list/finish")
    public AjaxResult getProcInstFinish(  TaskFinshDTO taskFinshDTO) {
        Map<String, Object> result = processInstanceService.getProcInstFinish(taskFinshDTO);
        return AjaxResult.success(result);
    }


    @ApiOperation("返回申请")
    @DeleteMapping("/cancel/apply")
    public AjaxResult cancelApply(@RequestParam String businessKey,@RequestParam String businessType,
                              @RequestParam String procInstId,
                              @RequestParam(defaultValue = "返回成功") String message) {
        processInstanceService.cancel(businessKey,businessType, procInstId, message);
        return AjaxResult.success();
    }

    /**
     * 本人发起的流程
     * @return
     */
    @GetMapping("/getCreateActiviti")
    public AjaxResult getCreateActiviti(){
        List<HistoricProcessInstance> historicProcessInstanceList = historyService.createHistoricProcessInstanceQuery().startedBy(SecurityUtils.getUserId()+"").list();
        return AjaxResult.success(historicProcessInstanceList);
    }

}
